using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using WeMakeSP.Platform.SharePoint.WebParts;

namespace WeMakeSP.WebComponents.RSSAdvancedReaderWebPart
{
    /// <summary>
    /// Represents the RSS advanced web part.
    /// </summary>
    [DefaultProperty("Text"),
    ToolboxData("<{0}:RSSAdvancedReader runat=server></{0}:RSSAdvancedReader>"),
    XmlRoot(Namespace = "WeMakeSP.WebComponents.RSSAdvancedReaderWebPart")]
    public class RSSAdvancedReaderWebPart : UserControlWebPart<RSSAdvancedReader>
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="RSSAdvancedReaderWebPart"/> class.
        /// </summary>
        public RSSAdvancedReaderWebPart()
        {
            this.Initialize();
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets a value indicating whether [asynchronous load].
        /// </summary>
        /// <value><c>True</c> if [asynchronous load]; otherwise, <c>false</c>.</value>
        [WebBrowsable(true)]
        [Category("RSS")]
        [WebDisplayName("Asynchronous Load")]        
        [Personalizable(PersonalizationScope.Shared)]        
        public bool AsynchronousLoad
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the auto refresh interval.
        /// </summary>
        /// <value>The auto refresh interval.</value>
        [WebBrowsable(true)]
        [Category("RSS")]
        [Personalizable(PersonalizationScope.Shared)]
        [WebDisplayName("Auto Refresh Interval")]
        [WebDescription("Indicates the time between automatic feeds refresh ( in seconds).")]
        public int AutoRefreshInterval
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the feeds.
        /// </summary>
        /// <value>The feeds.</value>
        [WebBrowsable(true)]
        [Category("RSS")]
        [Personalizable(PersonalizationScope.Shared)]
        [WebDisplayName("Feeds")]
        [WebDescription("The list of rss feeds separated using semicolons.")]
        public string Feeds
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the item view.
        /// </summary>
        /// <value>The item view.</value>
        [WebBrowsable(true)]
        [Category("RSS")]
        [WebDisplayName("Item view")]
        [WebDescription("The view mode of the feed items.")]
        [Personalizable(PersonalizationScope.User)]
        public RSSAdvancedReaderItemView ItemView
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the limit.
        /// </summary>
        /// <value>The limit.</value>
        [WebBrowsable(true)]
        [Category("RSS")]
        [Personalizable(PersonalizationScope.Shared)]
        [WebDisplayName("Limit")]
        [WebDescription("The maximum displayed entries per feed.")]
        public int Limit
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the layout.
        /// </summary>
        /// <value>The layout.</value>
        [WebBrowsable(true)]
        [Category("RSS")]
        [Personalizable(PersonalizationScope.User)]
        [WebDisplayName("Orientation")]
        [WebDescription("The feeds orientation.")]
        public RSSAdvancedReaderOrientation Orientation
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether [show publication date].
        /// </summary>
        /// <value><c>True</c> if [show publication date]; otherwise, <c>false</c>.</value>
        [WebBrowsable(true)]
        [Category("RSS")]
        [Personalizable(PersonalizationScope.User)]
        [WebDisplayName("Show Publication Date")]
        [WebDescription("Indicates if the publication date is diplayed on each item.")]
        public bool ShowPublicationDate
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether [show feed image].
        /// </summary>
        /// <value><c>True</c> if [show feed image]; otherwise, <c>false</c>.</value>
        [WebBrowsable(true)]
        [Category("RSS")]
        [Personalizable(PersonalizationScope.User)]
        [WebDisplayName("Show Feed Image")]
        [WebDescription("Indicates if the feed image is displayed instead of the title.")]
        public bool ShowFeedImage
        {
            get;
            set;
        }

        #endregion

        #region Protected Methods

        /// <summary>
        /// Sets the web part properties.
        /// </summary>
        protected override void SetWebPartProperties()
        {
            try
            {
                base.SetWebPartProperties();

                this.UserControl.AsynchronousLoad = this.AsynchronousLoad;
                this.UserControl.AutoRefreshInterval = this.AutoRefreshInterval;
                this.UserControl.Feeds = this.Feeds;
                this.UserControl.ItemView = this.ItemView;
                this.UserControl.Limit = this.Limit;
                this.UserControl.Orientation = this.Orientation;
                this.UserControl.ShowFeedImage = this.ShowFeedImage;
                this.UserControl.ShowPublicationDate = this.ShowPublicationDate;
            }
            catch (Exception ex)
            {
                if (this.HandlePresentationLayerException(ex) == false)
                {
                    throw;
                }
            }
        }

        /// <summary>
        /// Subscribes to events.
        /// </summary>
        protected override void SubscribeToEvents()
        {
            base.SubscribeToEvents();

            this.UserControl.PreRender += new EventHandler(this.UserControlPreRenderEventHandler);
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Initializes this instance.
        /// </summary>
        private void Initialize()
        {
            this.ExportMode = WebPartExportMode.All;

            // Set the user control location.

            this.UserControlPath = 
                "~/_controltemplates/RSSAdvancedReader/RSSAdvancedReader.ascx";

            // Indicate that the web part needs AJAX.

            this.AjaxEnabled = true;
        }

        /// <summary>
        /// Users the control pre render event handler.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void UserControlPreRenderEventHandler(object sender, EventArgs e)
        {
        }

        #endregion
    }
}
